<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/7 0007
 * Time: 下午 5:48
 */
namespace app\api\controller;

use app\api\model\StoreMember;
use app\common\BaseHelper;
use app\common\validate\AddmemberValidate;
use think\Controller;
use think\Exception;
use think\facade\Request;

class Setting extends Base{
    /**
     * 添加门店员工
     * @return \think\response\Json
     */
    public function addmember(){

        (new AddmemberValidate())->goCheck();

        if(Request::isPost()){
            $param = Request::param();
            if(!empty($param['id'])){
                $model = StoreMember::where('id',$param['id'])->find();
            }else{
                $count = db('store_member')->where('cashier_account',$param['cashier_account'])->count();
                if($count > 0){
                    return $this->jsonData(2,'此账号已经注册');
                }
                $model = new StoreMember();
                if(empty(trim($param['cashier_pwd']))){
                    return $this->jsonData(2,'必须填密码');
                }
            }
            $model->store_id = $param['store_id'];
            $model->name = $param['name'] ?? '';
            $model->cashier_account = $param['cashier_account'];
            if(!empty(trim($param['cashier_pwd']))){
                $model->cashier_pwd = BaseHelper::password_hash($param['cashier_pwd']);
            }
            $model->role_id = $param['role_id'];
            try{
                $model->save();
                return $this->jsonSuccessData();
            }catch (Exception $e){
                return $this->jsonData(2,'系统异常');
            }
        }else{
            return $this->jsonData(2,'非法请求');
        }

    }
}